/*
	File:		OptionState.cpp
	Course:		SGD 1301
	Author:		Peter Nguyen
	Purpose:	COptionState class handles the option screen
*/

#include "OptionState.h"

#include "../SGD Wrappers/CSGD_DirectInput.h"
#include "../SGD Wrappers/CSGD_Direct3D.h"
#include "../SGD Wrappers/CSGD_TextureManager.h"
#include "../SGD Wrappers/CSGD_XAudio2.h"

#include "Game.h"
#include "GameplayState.h"
#include "BitmapFont.h"
#include "MainMenuState.h"

#include <cassert>

#include <iostream>
#include <fstream>

////////////////////////////////////////////////////////////
// SINGLETON!
/*static*/ COptionState* COptionState::GetInstance( void )
{
	// Static local object
	static COptionState s_Instance;

	return &s_Instance;
}


////////////////////////////////////////////////////////////
// CONSTRUCTOR
COptionState::COptionState(void)
{
	m_pDI		= nullptr;
	m_pTM		= nullptr;
	m_pFont		= nullptr;
	
	
	m_nCursorImageID	= -1;
	
	m_nCursorPos		= 0;
}


////////////////////////////////////////////////////////////
// DESTRUCTOR
COptionState::~COptionState(void)
{
}


////////////////////////////////////////////////////////////
// Enter				- load resources
/*virtual*/ void COptionState::Enter( void )
{
	// Store wrappers
	m_pXA			= CSGD_XAudio2::GetInstance();
	m_pDI		= CSGD_DirectInput::GetInstance();
	m_pTM		= CSGD_TextureManager::GetInstance();
	m_pFont		= CGame::GetInstance()->GetFont();
	
	// Load assets
	m_nCursorImageID	= m_pTM->LoadTexture( 
							_T("resource/graphics/PN_MenuCursor.png") );
	
	
	m_nBackgroundMusicID	= m_pXA->MusicLoadSong( _T("resource/sounds/PN_MUS_Background.xwm") );
	m_nLaserShotSfxID		= m_pXA->SFXLoadSound( _T("resource/sounds/PN_SFX_LaserShot.wav") );
	
	m_pXA->MusicSetSongVolume(m_nBackgroundMusicID,CGame::GetInstance()->GetBGMVolume()/100.0f);
	m_pXA->SFXSetSoundVolume(m_nLaserShotSfxID,CGame::GetInstance()->GetSoundVolume()/100.0f);

	// Set other members
	m_nCursorPos		= 0;
	m_nMenuSize			= 3;
}

////////////////////////////////////////////////////////////
// Exit					- unload resources
/*virtual*/ void COptionState::Exit( void )
{
	UnloadTextureHelper(m_nCursorImageID);
	UnloadMusicHelper(m_nBackgroundMusicID);
	UnloadSFXHelper(m_nLaserShotSfxID);

	// Reset the wrappers to null
	m_pDI		= nullptr;
	m_pTM		= nullptr;
	m_pXA		= nullptr;
	m_pFont		= nullptr;

	SaveSoundSettingToFile();
}

void COptionState::SaveSoundSettingToFile()
{
	CGame * gamePtr = CGame::GetInstance();
	int nSFX = gamePtr->GetSoundVolume();
	int nBGM = gamePtr->GetBGMVolume();

	std::ofstream myFile;
	myFile.open("settings.txt");
	if(myFile.is_open())
	{
		myFile << "" << nSFX << '\n';
		myFile << "" << nBGM << '\n';
		myFile.close();
	}
}

////////////////////////////////////////////////////////////
// Input			- handle user input
/*virtual*/ bool COptionState::Input( void )
{
	CGame * gamePtr = CGame::GetInstance();
	
	if( m_pDI->KeyPressed( DIK_LEFT ) )
	{
		if(m_nCursorPos==0)
		{
			gamePtr->SetSoundVolume(gamePtr->GetSoundVolume()-5);	
			m_pXA->SFXSetSoundVolume(m_nLaserShotSfxID,CGame::GetInstance()->GetSoundVolume()/100.0f);
			m_pXA->SFXPlaySound(m_nLaserShotSfxID);
		}
		else if(m_nCursorPos==1)
		{
			gamePtr->SetBGMVolume(gamePtr->GetBGMVolume()-5);
			m_pXA->MusicSetSongVolume(m_nBackgroundMusicID,CGame::GetInstance()->GetBGMVolume()/100.0f);
		}
	}

	if( m_pDI->KeyPressed( DIK_RIGHT ) )
	{
		if(m_nCursorPos==0)
		{
			gamePtr->SetSoundVolume(gamePtr->GetSoundVolume()+5);
			m_pXA->SFXSetSoundVolume(m_nLaserShotSfxID,CGame::GetInstance()->GetSoundVolume()/100.0f);
			m_pXA->SFXPlaySound(m_nLaserShotSfxID);
		}
		else if(m_nCursorPos==1)
			gamePtr->SetBGMVolume(gamePtr->GetBGMVolume()+5);
			m_pXA->MusicSetSongVolume(m_nBackgroundMusicID,CGame::GetInstance()->GetBGMVolume()/100.0f);
	}


	if( m_pDI->KeyPressed( DIK_UP ) )
	{
		--m_nCursorPos;
		if(m_nCursorPos<0)
			m_nCursorPos = m_nMenuSize - 1;
		if(m_nCursorPos == 1)
		{
			m_pXA->MusicStopSong(m_nBackgroundMusicID);
			m_pXA->MusicPlaySong( m_nBackgroundMusicID, true );
		}
		else if(m_nCursorPos != 1)	
			m_pXA->MusicStopSong(m_nBackgroundMusicID);
		if(m_nCursorPos == 0) 
			m_pXA->SFXPlaySound(m_nLaserShotSfxID);

	}
	else if( m_pDI->KeyPressed( DIK_DOWN ) )
	{
		++m_nCursorPos;
		if(m_nCursorPos == m_nMenuSize)
			m_nCursorPos = 0;
		if(m_nCursorPos == 1)
		{
			m_pXA->MusicStopSong(m_nBackgroundMusicID);
			m_pXA->MusicPlaySong( m_nBackgroundMusicID, true );
		}
		else if(m_nCursorPos != 1)	
			m_pXA->MusicStopSong(m_nBackgroundMusicID);
		if(m_nCursorPos == 0) 
			m_pXA->SFXPlaySound(m_nLaserShotSfxID);
	}
	if( m_pDI->KeyPressed( DIK_ESCAPE ) )
	{
		CGame::GetInstance()->PopState();
		return true;
	}

	if( m_pDI->KeyPressed( DIK_RETURN ) )
	{
		switch( m_nCursorPos )
		{
		case 0:		// Play Game
			{
				return true;
			}
			break;
			
		case 1:		// How To Play
			{
				return true;
			}
			break;
		case 2:		// Back
			{
			CGame::GetInstance()->PopState();
				return true;
			}
			break;
		}
	}


	return true;
}				

////////////////////////////////////////////////////////////
// Update			- update game entities
/*virtual*/ void COptionState::Update( float fElapsedTime )
{
}

////////////////////////////////////////////////////////////
// Render			- render game entities
/*virtual*/ void COptionState::Render( void )
{
	CGame * gamePtr = CGame::GetInstance();
	// Render the menu
	m_pFont->Draw( _T("Options"), 100, 100, 1.0f, D3DCOLOR_ARGB( 255, 255, 255, 255 ) );

	int space = 50;
	int start = 200;
	int current = start;

	
	TOSTRINGSTREAM output;
	
	output << _T("SFX - Volume - ") << gamePtr->GetSoundVolume();
	m_pFont->Draw(output.str().c_str(),		100, current+=space, 1.0f, D3DCOLOR_ARGB( 255, 255, 255, 255 ) );
	output.str(_T(""));

	output << _T("BGM - Volume - ") << gamePtr->GetBGMVolume();
	m_pFont->Draw(output.str().c_str(),		100, current+=space, 1.0f, D3DCOLOR_ARGB( 255, 255, 255, 255 ) );
	output.str(_T(""));

	m_pFont->Draw( _T("Back"),	100, current+=space, 1.0f, D3DCOLOR_ARGB( 255, 255, 255, 255 ) );

	// Render the cursor
	int nCursorY = (int)(start + space*(m_nCursorPos)+space*1.2f);
	m_pTM->Draw( m_nCursorImageID, space, nCursorY );
}
	

void COptionState::UnloadTextureHelper(int&id)
{
	if(id!=-1)
	{
		m_pTM->UnloadTexture(id);
		id = -1;
	}
}
void COptionState::UnloadMusicHelper(int&id)
{
	if( id != -1 )
	{
		m_pXA->MusicStopSong( id );
		m_pXA->MusicUnloadSong( id );
		id = -1;
	}
}
void COptionState::UnloadSFXHelper(int&id)
{
	if( id != -1 )
	{
		m_pXA->SFXStopSound( id );
		m_pXA->SFXUnloadSound( id );
		id = -1;
	}
}
